package com.example.demo.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.common.Result;
import com.example.demo.entity.Fronter;
import com.example.demo.mapper.FronterMapper;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.sql.Wrapper;

@RestController
@RequestMapping("/frontermanager")
public class FronterController {

    @Resource
    FronterMapper fronterMapper;

    @PostMapping
    public Result<?> save(@RequestBody Fronter fronter){
        fronterMapper.insert(fronter);
        return Result.success();
    }

    @PutMapping
    public Result<?> update(@RequestBody Fronter fronter){
        fronterMapper.updateById(fronter);
        return Result.success();
    }

    @DeleteMapping("/{fronterid}")
    public Result<?> update(@PathVariable long fronterid){
        fronterMapper.deleteById(fronterid);
        return Result.success();
    }

    @GetMapping
    public Result<?> findPage(@RequestParam(defaultValue = "1") Integer pageNum,
                              @RequestParam(defaultValue = "10") Integer pageSize,
                              @RequestParam(defaultValue = "") String search){
        LambdaQueryWrapper<Fronter> wrapper = Wrappers.<Fronter>lambdaQuery();
        if (StrUtil.isNotBlank(search)) {
            wrapper.like(Fronter::getFronterid, search);
        }
        Page<Fronter> fronterPage = fronterMapper.selectPage(new Page<>(pageNum, pageSize), wrapper);
        return Result.success(fronterPage);
    }
}
